﻿Public Class FormationInteret
    Dim oLocalContext As DBMainContext
    Dim localBeneficiaire As Beneficiaire

    Sub New(ByVal oBenef As Beneficiaire)

        InitializeComponent()
        localBeneficiaire = oBenef
        oLocalContext = New DBMainContext
        initialize()

    End Sub

    Sub initialize()

        If Not localBeneficiaire.ID = 0 Then
            lbInteret.DataSource = (From u In oLocalContext.Interet_Beneficiaire Join v In oLocalContext.Interet On u.ID_Interet Equals v.ID_Interet Where u.ID_Beneficiaire = localBeneficiaire.ID Select v).ToList
            lbInteret.DisplayMember = "Nom"
        End If

        If Not localBeneficiaire.ID = 0 Then
            lbFormation.DataSource = (From u In oLocalContext.Formation_Beneficiaire Join v In oLocalContext.Formation On u.ID_Formation Equals v.ID Where u.ID_Beneficiaire = localBeneficiaire.ID Select v).ToList
            lbFormation.DisplayMember = "Nom"
        End If

    End Sub
    Private Sub btnAjoutFormation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjoutFormation.Click
        Dim oNewFormation As New GestionFormation()
        Dim oCustomMessagebox As New CustomMessageBox
        oNewFormation.btnSauvegarder.Visible = False
        oNewFormation.dgvFormation.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        oNewFormation.oListFormation = (From u In oLocalContext.Formation_Beneficiaire Join v In oLocalContext.Formation On u.ID_Formation Equals v.ID Where u.ID_Beneficiaire = localBeneficiaire.ID Select v).ToList

        oCustomMessagebox.Height = oNewFormation.Height + 90
        oCustomMessagebox.Width = oNewFormation.Width
        oCustomMessagebox.Controls.Add(oNewFormation)
        oCustomMessagebox.ShowDialog()

        If oCustomMessagebox.DialogResult = DialogResult.OK Then
            localBeneficiaire = (From u In oLocalContext.Beneficiaire Where u.ID = localBeneficiaire.ID Select u).FirstOrDefault
            For Each item In oNewFormation.GetSelectedItems.ToArray
                If Not item Is Nothing Then
                    localBeneficiaire.Formation_Beneficiaire.Add(New Formation_Beneficiaire With {.ID_Formation = item.ID})
                End If

            Next
            oLocalContext.CustomSubmitChanges()
            If Not localBeneficiaire.ID = 0 Then
                lbFormation.DataSource = (From u In oLocalContext.Formation_Beneficiaire Join v In oLocalContext.Formation On u.ID_Formation Equals v.ID Where u.ID_Beneficiaire = localBeneficiaire.ID Select v).ToList
                lbFormation.DisplayMember = "Nom"
            End If
        End If
    End Sub

    Private Sub btnAjoutInteret_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAjoutInteret.Click
        Dim oNewForm As New GestionInterets()
        Dim oCustomMessageBox As New CustomMessageBox
        oNewForm.btnSauvegarder.Visible = False
        oNewForm.dgvInteret.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        oNewForm.oListInteretDejaDansLaListe = (From u In oLocalContext.Interet_Beneficiaire Join v In oLocalContext.Interet On u.ID_Interet Equals v.ID_Interet Where u.ID_Beneficiaire = localBeneficiaire.ID Select v).ToList

        oCustomMessageBox.Height = oNewForm.Height + 90
        oCustomMessageBox.Width = oNewForm.Width
        oCustomMessageBox.Controls.Add(oNewForm)
        oCustomMessageBox.ShowDialog()

        If oCustomMessageBox.DialogResult = DialogResult.OK Then
            localBeneficiaire = (From u In oLocalContext.Beneficiaire Where u.ID = localBeneficiaire.ID Select u).FirstOrDefault
            For Each item In oNewForm.GetSelectedItems.ToArray
                If Not item Is Nothing Then
                    localBeneficiaire.Interet_Beneficiaire.Add(New Interet_Beneficiaire With {.ID_Interet = item.ID_Interet})
                End If

            Next
            oLocalContext.CustomSubmitChanges()

            If Not localBeneficiaire.ID = 0 Then
                lbInteret.DataSource = (From u In oLocalContext.Interet_Beneficiaire Join v In oLocalContext.Interet On u.ID_Interet Equals v.ID_Interet Where u.ID_Beneficiaire = localBeneficiaire.ID Select v).ToList
                lbInteret.DisplayMember = "Nom"
            End If
        End If

    End Sub

    Private Sub btnSuppInteret_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuppInteret.Click
        If Not lbInteret.Text.Trim = "" Then
            Dim oInteret_Beneficiaire As Interet_Beneficiaire
            oInteret_Beneficiaire = (From u In oLocalContext.Interet_Beneficiaire Where u.ID_Beneficiaire = localBeneficiaire.ID And u.ID_Interet = CType(lbInteret.SelectedItem, Interet).ID_Interet Select u).FirstOrDefault

            oLocalContext.Interet_Beneficiaire.DeleteOnSubmit(oInteret_Beneficiaire)
            oLocalContext.CustomSubmitChanges()

            If Not localBeneficiaire.ID = 0 Then
                lbInteret.DataSource = (From u In oLocalContext.Interet_Beneficiaire Join v In oLocalContext.Interet On u.ID_Interet Equals v.ID_Interet Where u.ID_Beneficiaire = localBeneficiaire.ID Select v).ToList
                lbInteret.DisplayMember = "Nom"
            End If
        End If
    End Sub

    Private Sub btnSuppFormation_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSuppFormation.Click
        If Not lbFormation.Text.Trim = "" Then
            Dim oFormation_Beneficiaire As Formation_Beneficiaire
            oFormation_Beneficiaire = (From u In oLocalContext.Formation_Beneficiaire Where u.ID_Beneficiaire = localBeneficiaire.ID And u.ID_Formation = CType(lbFormation.SelectedItem, Formation).ID Select u).FirstOrDefault

            oLocalContext.Formation_Beneficiaire.DeleteOnSubmit(oFormation_Beneficiaire)
            oLocalContext.CustomSubmitChanges()

            If Not localBeneficiaire.ID = 0 Then
                lbFormation.DataSource = (From u In oLocalContext.Formation_Beneficiaire Join v In oLocalContext.Formation On u.ID_Formation Equals v.ID Where u.ID_Beneficiaire = localBeneficiaire.ID Select v).ToList
                lbFormation.DisplayMember = "Nom"
            End If
        End If
    End Sub
End Class
